package com.zc.mychart.chart;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;

import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Utils;
import com.zc.mychart.R;
import com.zc.mychart.model.GoodsAllBean;
import com.zc.mychart.model.KTimeType;
import com.zc.mychart.model.KTrainRect;
import com.zc.mychart.model.KViewMode;
import com.zc.mychart.model.LineType;
import com.zc.mychart.utils.MyTimer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/28 0028.
 */

public class MyKView extends RelativeLayout implements RadioGroup.OnCheckedChangeListener {
    private View mRootView, rl_parent, rl_right, rl_loading, view_center, view_line1;
    private View rl_mmxh_cover, rl_hjgd_cover;
    private RadioGroup rg_time_type, rg_k_type, rg_bar_type, rg_min_time_type;
    private RadioGroup rg_time_type_horizontal, rg_k_type_right, rg_bar_type_right;
    private RadioButton rb_hjgd_right, rb_mmxh_right, rbm_5_day, rbm_1_month, rbm_1_min;
    private MyKChart mKChart; //蜡烛图
    private MyBarChart mBarChart; //MACD图
    private MyLineChart mLineChart; //分时图
    private List<Entry> min1Entries;//分时图缓存数据
    private Map<KTimeType, List<CandleEntry>> kMap = new HashMap<>();//K线图缓存数据
    private KTimeType mTimeType = KTimeType.MIN1;    //分时、1分、5分...日线
    private LineType mKlineType = LineType.SMA;     //SMA  EMA BOLL
    private LineType mBarLineType = LineType.MACD;   //MACD KDJ RSI
    private MyChartListener myChartListener;
    private MyChartCallBack myChartCallBack;
    private RadioGroup.OnCheckedChangeListener changeListener;
    private MyTimer myTimer;
    private long mCountDownInterval = 30000;
    private boolean mLinearChartToucher = false;
    private KViewMode mKlineMode = KViewMode.MODE_KLINE;
    private int multiple = 1000;

    private Context mContext;

    private int radioTopBgColor;
    private int radioBottomBgColor;
    private int bgColor;
    private int lineColor;
    private int textColor;
    private int radioTextColor;
    private int rightBorder;

    private boolean isLight = true;
    private GoodsAllBean mGroupBean;

    public MyKView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mRootView = LayoutInflater.from(context).inflate(R.layout.k_view, this, true);
    }

    public void init(MyChartListener listener, KViewMode kMode) {
        myChartListener = listener;
        mKlineMode = kMode;
        findViews();
        setListeners();
        initView();
        setOffset();
        loading();
        if (mKlineMode == KViewMode.MODE_TRAIN) {
            mTimeType = KTimeType.MIN5;
            rb_hjgd_right.setVisibility(GONE);
            rb_mmxh_right.setVisibility(GONE);
        }
    }

    public void onResume() {
        startTimer();
    }

    public void onPause() {
        stopTimer();
    }

    public void setOrientation(boolean isVertical) {
        if (isVertical) {
            turnToVertical();
        } else {
            turnToHorizontal();
        }
    }

    public void setLightBg() {
        isLight = true;
        radioTopBgColor = ContextCompat.getColor(mContext, R.color.mychart_white);
        radioBottomBgColor = ContextCompat.getColor(mContext, R.color.mychart_white_background);
        bgColor = ContextCompat.getColor(mContext, R.color.mychart_white_background);
        textColor = ContextCompat.getColor(mContext, R.color.mychart_text_main);
        radioTextColor = R.drawable.text_color_red_gray_selector;
        lineColor = ContextCompat.getColor(mContext, R.color.mychart_gray_k_line);
//        rg_min_time_type.setBackgroundResource(R.drawable.border_gray_empty_1px);
        rightBorder = R.drawable.border_gray_k_line;
        setColors();
        mLineChart.setLightBg();
        mKChart.setLightBg();
        mBarChart.setLightBg();
        rl_mmxh_cover.setBackgroundResource(R.drawable.img_mmxh_white_bg);
        rl_hjgd_cover.setBackgroundResource(R.drawable.img_hjgd_white_bg);


        rbm_5_day.setBackgroundResource(R.drawable.radio_min_type_selector);
        rbm_1_month.setBackgroundResource(R.drawable.radio_min_type_selector);
        rbm_1_min.setBackgroundResource(R.drawable.radio_min_type_selector);
    }

    public void setDarkBg() {
        isLight = false;
        radioTopBgColor = ContextCompat.getColor(mContext, R.color.mychart_bg_time_type);
        radioBottomBgColor = ContextCompat.getColor(mContext, R.color.mychart_background);
        bgColor = ContextCompat.getColor(mContext, R.color.mychart_background);
//        rg_min_time_type.setBackgroundResource(R.drawable.border_gray_empty_1px);
        textColor = ContextCompat.getColor(mContext, R.color.mychart_white);
        radioTextColor = R.drawable.text_color_red_white_selector;
        lineColor = ContextCompat.getColor(mContext, R.color.mychart_k_line);
        rightBorder = R.drawable.border_k_line;
        setColors();
        mLineChart.setDarkBg();
        mKChart.setDarkBg(mKlineMode);
        mBarChart.setDarkBg(mKlineMode);
        rl_mmxh_cover.setBackgroundResource(R.drawable.img_mmxh_black_bg);
        rl_hjgd_cover.setBackgroundResource(R.drawable.img_hjgd_black_bg);

        rbm_5_day.setBackgroundResource(R.drawable.radio_min_type_dark_selector);
        rbm_1_month.setBackgroundResource(R.drawable.radio_min_type_dark_selector);
        rbm_1_min.setBackgroundResource(R.drawable.radio_min_type_dark_selector);
    }

    private void setColors() {
        rl_parent.setBackgroundColor(bgColor);
        rg_k_type.setBackgroundColor(bgColor);
        rg_bar_type.setBackgroundColor(bgColor);
        rl_loading.setBackgroundColor(bgColor);
        rg_time_type.setBackgroundColor(radioTopBgColor);
        rg_time_type_horizontal.setBackgroundColor(radioTopBgColor);
        rl_right.setBackgroundResource(rightBorder);
        view_center.setBackgroundColor(lineColor);
        view_line1.setBackgroundColor(lineColor);

        int timeRadioCount = rg_time_type.getChildCount();
        for (int i = 0; i < timeRadioCount; i++) {
            ((RadioButton) rg_time_type.getChildAt(i)).setTextColor(textColor);
            ((RadioButton) rg_time_type_horizontal.getChildAt(i)).setTextColor(textColor);
            if (isLight) {
                rg_time_type.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
                rg_time_type_horizontal.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
            } else {
                rg_time_type.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
                rg_time_type_horizontal.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
            }
        }

        int kRadioCount = rg_k_type.getChildCount();
        for (int i = 0; i < kRadioCount; i++) {
            ((RadioButton) rg_k_type.getChildAt(i)).setTextColor(textColor);
            ColorStateList csl = ContextCompat.getColorStateList(mContext, radioTextColor);
            ((RadioButton) rg_k_type_right.getChildAt(i)).setTextColor(csl);
            if (isLight) {
                rg_k_type.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
            } else {
                rg_k_type.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
            }
        }

        int barRadioCount = rg_bar_type.getChildCount();
        for (int i = 0; i < barRadioCount; i++) {
            ((RadioButton) rg_bar_type.getChildAt(i)).setTextColor(textColor);
            ColorStateList csl = ContextCompat.getColorStateList(mContext, radioTextColor);
            ((RadioButton) rg_bar_type_right.getChildAt(i)).setTextColor(csl);
            if (isLight) {
                rg_bar_type.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
            } else {
                rg_bar_type.getChildAt(i).setBackgroundResource(R.drawable.radio_k_type_selector);
            }
        }
    }

    private void findViews() {
        rl_parent = mRootView.findViewById(R.id.rl_parent);
        rl_loading = mRootView.findViewById(R.id.rl_loading);
        rl_right = mRootView.findViewById(R.id.rl_right);
        view_center = mRootView.findViewById(R.id.view_center);
        view_line1 = mRootView.findViewById(R.id.view_line1);
        rg_time_type = mRootView.findViewById(R.id.rg_time_type);
        rg_min_time_type = mRootView.findViewById(R.id.rg_min_time_type);
        rg_k_type = mRootView.findViewById(R.id.rg_k_type);
        rbm_5_day = mRootView.findViewById(R.id.rbm_5_day);
        rbm_1_min = mRootView.findViewById(R.id.rbm_1_min);
        rbm_1_month = mRootView.findViewById(R.id.rbm_1_month);
        rg_bar_type = mRootView.findViewById(R.id.rg_bar_type);
        rg_time_type_horizontal = mRootView.findViewById(R.id.rg_time_type_horizontal);
        rg_k_type_right = mRootView.findViewById(R.id.rg_k_type_right);
        rg_bar_type_right = mRootView.findViewById(R.id.rg_bar_type_right);
        rb_hjgd_right = (RadioButton) mRootView.findViewById(R.id.rb_hjgd_right);
        rb_mmxh_right = (RadioButton) mRootView.findViewById(R.id.rb_mmxh_right);
        mKChart = (MyKChart) mRootView.findViewById(R.id.mKChart);
        mBarChart = (MyBarChart) mRootView.findViewById(R.id.mBarChart);
        mLineChart = (MyLineChart) mRootView.findViewById(R.id.mLineChart);
        rl_mmxh_cover = mRootView.findViewById(R.id.rl_mmxh_cover);
        rl_hjgd_cover = mRootView.findViewById(R.id.rl_hjgd_cover);
    }

    private void setListeners() {
        rg_time_type.setOnCheckedChangeListener(getCheckedChangeListener());
        rg_k_type.setOnCheckedChangeListener(getCheckedChangeListener());
        rg_bar_type.setOnCheckedChangeListener(getCheckedChangeListener());
        rg_min_time_type.setOnCheckedChangeListener(getCheckedChangeListener());
        rg_time_type_horizontal.setOnCheckedChangeListener(this);
        rg_k_type_right.setOnCheckedChangeListener(this);
        rg_bar_type_right.setOnCheckedChangeListener(this);
//        rg_min_time_type.setOnCheckedChangeListener(this);
        mKChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                mBarChart.highlightValues(new Highlight[]{h});
            }

            @Override
            public void onNothingSelected() {
                mBarChart.highlightValues(new Highlight[]{null});
            }
        });
        rl_mmxh_cover.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myChartListener.goImageUrl();
            }
        });
        rl_hjgd_cover.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myChartListener.goImageUrl();
            }
        });

    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        mBarChart.initView();
        mKChart.initView();
        mLineChart.initView();
        mKChart.setMyBarChart(mBarChart);
        mKChart.setOnHighLightListener(mBarChart.getLightListener());
        mKChart.setOnChartGestureListener(new CoupleChartGestureListener(mKChart, new Chart[]{mBarChart}));
        mBarChart.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mKChart.dispatchTouchEvent(event);
                return true;
            }

        });
        mLineChart.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        mLinearChartToucher = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        mLinearChartToucher = false;
                        break;
                }
                return false;
            }
        });
    }

    private void setOffset() {
        float lineLeft = mKChart.getViewPortHandler().offsetLeft();
        float barLeft = mBarChart.getViewPortHandler().offsetLeft();
        float lineRight = mKChart.getViewPortHandler().offsetRight();
        float barRight = mBarChart.getViewPortHandler().offsetRight();
        float offsetLeft, offsetRight;
        /*注：setExtraLeft...函数是针对图表相对位置计算，比如A表offLeftA=20dp,B表offLeftB=30dp,则A.setExtraLeftOffset(10),并不是30，还有注意单位转换*/
        if (barLeft < lineLeft) {
            offsetLeft = Utils.convertPixelsToDp(lineLeft - barLeft);
            mBarChart.setExtraLeftOffset(offsetLeft);
        } else {
            offsetLeft = Utils.convertPixelsToDp(barLeft - lineLeft);
            mKChart.setExtraLeftOffset(offsetLeft);
        }
        /*注：setExtraRight...函数是针对图表绝对位置计算，比如A表offRightA=20dp,B表offRightB=30dp,则A.setExtraLeftOffset(30),并不是10，还有注意单位转换*/
        if (barRight < lineRight) {
            offsetRight = Utils.convertPixelsToDp(lineRight);
            mBarChart.setExtraRightOffset(offsetRight);
        } else {
            offsetRight = Utils.convertPixelsToDp(barRight);
            mKChart.setExtraRightOffset(offsetRight);
        }
    }

    private void startTimer() {
        stopTimer();
        myTimer = new MyTimer(Long.MAX_VALUE, mCountDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (myChartListener != null) {
                    myChartListener.getKLineData(mTimeType, getCallBack());
                }
            }
        };
        myTimer.start();
    }

    private void stopTimer() {
        if (myTimer != null) {
            myTimer.finishTimer();
            myTimer = null;
        }
    }

    private MyChartCallBack getCallBack() {
        if (myChartCallBack == null) {
            myChartCallBack = new MyChartCallBack() {
                @Override
                public void onSuccess(List<Entry> list, double close, double high, double low, KTimeType type) {
                    setLineData(list, close, high, low, type);
                }

                @Override
                public void onSuccess(List<CandleEntry> list, KTimeType type) {
                    setKData(list, type);
                }

                @Override
                public void onFailed(KTimeType type) {
                    if (type == KTimeType.MIN1) {
                        setLineData(null, 0, 0, 0, type);
                    } else {
                        setKData(null, type);
                    }
                }
            };
        }
        return myChartCallBack;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        Log.d("ret", "onCheckedChanged");
        if (checkedId == R.id.rb_1_min_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(0)).setChecked(true);
        } else if (checkedId == R.id.rb_1_k_min_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(1)).setChecked(true);
        } else if (checkedId == R.id.rb_5_min_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(2)).setChecked(true);
        } else if (checkedId == R.id.rb_15_min_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(3)).setChecked(true);
        } else if (checkedId == R.id.rb_30_min_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(4)).setChecked(true);
        } else if (checkedId == R.id.rb_60_min_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(5)).setChecked(true);
        } else if (checkedId == R.id.rb_hour_4_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(6)).setChecked(true);
        } else if (checkedId == R.id.rb_day_horizontal) {
            ((RadioButton) rg_time_type.getChildAt(7)).setChecked(true);
        } else if (checkedId == R.id.rb_sma_right) {
            ((RadioButton) rg_k_type.getChildAt(0)).setChecked(true);
        } else if (checkedId == R.id.rb_ema_right) {
            ((RadioButton) rg_k_type.getChildAt(1)).setChecked(true);
        } else if (checkedId == R.id.rb_boll_right) {
            ((RadioButton) rg_k_type.getChildAt(2)).setChecked(true);
        } else if (checkedId == R.id.rb_hjgd_right) {
            ((RadioButton) rg_k_type.getChildAt(3)).setChecked(true);
        } else if (checkedId == R.id.rb_macd_right) {
            ((RadioButton) rg_bar_type.getChildAt(0)).setChecked(true);
        } else if (checkedId == R.id.rb_kdj_right) {
            ((RadioButton) rg_bar_type.getChildAt(1)).setChecked(true);
        } else if (checkedId == R.id.rb_rsi_right) {
            ((RadioButton) rg_bar_type.getChildAt(2)).setChecked(true);
        } else if (checkedId == R.id.rb_mmxh_right) {
            ((RadioButton) rg_bar_type.getChildAt(3)).setChecked(true);
        } else if (checkedId == R.id.rbm_1_min) {
            ((RadioButton) rg_min_time_type.getChildAt(0)).setChecked(true);
        } else if (checkedId == R.id.rbm_5_day) {
            ((RadioButton) rg_min_time_type.getChildAt(1)).setChecked(true);
        } else if (checkedId == R.id.rbm_5_day) {
            ((RadioButton) rg_min_time_type.getChildAt(2)).setChecked(true);
        }
    }

    private RadioGroup.OnCheckedChangeListener getCheckedChangeListener() {
        if (changeListener == null) {
            changeListener = new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if (checkedId == R.id.rb_1_min) {
                        showChart(KTimeType.MIN1, 40 * 1000);
                    } else if (checkedId == R.id.rb_1_min_k) {
                        showChart(KTimeType.MIN_1, 50 * 1000);
                    } else if (checkedId == R.id.rb_5_min) {
                        showChart(KTimeType.MIN5, 4 * 60 * 1000);
                    } else if (checkedId == R.id.rb_15_min) {
                        showChart(KTimeType.MIN15, 13 * 60 * 1000);
                    } else if (checkedId == R.id.rb_30_min) {
                        showChart(KTimeType.MIN30, 25 * 60 * 1000);
                    } else if (checkedId == R.id.rb_60_min) {
                        showChart(KTimeType.HOUR, 50 * 60 * 1000);
                    } else if (checkedId == R.id.rb_hour_4) {
                        showChart(KTimeType.HOUR4, 3 * 60 * 60 * 1000);
                    } else if (checkedId == R.id.rb_day) {
                        showChart(KTimeType.DAY, 20 * 60 * 60 * 1000);
                    } else if (checkedId == R.id.rb_sma) {
                        checkKRadio(LineType.SMA);
                    } else if (checkedId == R.id.rb_ema) {
                        checkKRadio(LineType.EMA);
                    } else if (checkedId == R.id.rb_boll) {
                        checkKRadio(LineType.BOLL);
                    } else if (checkedId == R.id.rb_macd) {
                        checkBarRadio(LineType.MACD);
                    } else if (checkedId == R.id.rb_kdj) {
                        checkBarRadio(LineType.KDJ);
                    } else if (checkedId == R.id.rb_rsi) {
                        checkBarRadio(LineType.RSI);
                    } else if (checkedId == R.id.rb_mmxh) {
                        checkBarRadio(LineType.MMXH);
                    } else if (checkedId == R.id.rb_hjgd) {
                        checkKRadio(LineType.HJGD);
                    } else if (checkedId == R.id.rbm_1_min) {
                        showChart(KTimeType.MIN1, 40 * 1000);
                    } else if (checkedId == R.id.rbm_5_day) {
                        showChart(KTimeType.MINI7, 1000000000);
                    } else if (checkedId == R.id.rbm_1_month) {
                        showChart(KTimeType.MINI10, 1000000000);
                    }
                    showKBarCoverView();
                }
            };
        }
        return changeListener;
    }

    private void showKBarCoverView() {
        boolean kIndexOpen = false;
        if (mKlineType == LineType.HJGD) {
            if (kIndexOpen) {
                rl_hjgd_cover.setVisibility(GONE);
            } else {
                rl_hjgd_cover.setVisibility(VISIBLE);
            }
        } else {
            rl_hjgd_cover.setVisibility(GONE);
        }

        if (mBarLineType == LineType.MMXH) {
            if (kIndexOpen) {
                rl_mmxh_cover.setVisibility(GONE);
            } else {
                rl_mmxh_cover.setVisibility(VISIBLE);
            }
        } else {
            rl_mmxh_cover.setVisibility(GONE);
        }
    }

    private void setLineData(List<Entry> list, double close, double high, double low, KTimeType type) {
        if (mTimeType == type) {
            showLineChart();
            mLineChart.setLineData(list, close, high, low, mTimeType, mGroupBean,true);
            cancelLoading();
        }
    }

    public void setLineDataNoReset(List<Entry> list, double close, double high, double low, KTimeType type) {
        if (mTimeType == type) {
            showLineChart();
            mLineChart.setLineData(list, close, high, low, mTimeType, mGroupBean,false);
            cancelLoading();
        }
    }

    private void setKData(List<CandleEntry> list, KTimeType type) {
        if (type == mTimeType) {
            hideLineChart();
            kMap.put(mTimeType, list);
            mKChart.setCandleData(mKlineType, list, true);
            mBarChart.setBarData(mBarLineType, list, true);
            cancelLoading();
        }
    }

    public void refreshKData(List<CandleEntry> list, boolean isNeedSetData) {
//        if (KTimeType.DAY == mTimeType) {
        hideLineChart();
        kMap.put(mTimeType, list);
        mKChart.setCandleData(mKlineType, list, isNeedSetData);
        mBarChart.setBarData(mBarLineType, list, isNeedSetData);
        cancelLoading();
//        }
    }

    public List<CandleEntry> getCandleEntries() {
        List<CandleEntry> entries = new ArrayList<>();
        if (mKChart.getCandleData() != null && mKChart.getCandleData().getDataSetCount() > 0) {
            entries = mKChart.getCandleEntryList();
        }
        return entries;
    }

    private void showChart(KTimeType timeType, long interval) {
        mTimeType = timeType;
        mCountDownInterval = interval;
        loading();
        startTimer();
    }

    private void checkKRadio(LineType type) {
        mKlineType = type;
        mKChart.setLineData(mKlineType, kMap.get(mTimeType));
    }

    private void checkBarRadio(LineType type) {
        mBarLineType = type;
        mBarChart.setBarData(mBarLineType, kMap.get(mTimeType), false);
    }

    public void loading() {
//        if (rl_loading.getVisibility() == GONE) {
//            rl_loading.setVisibility(VISIBLE);
//        }
    }

    private void cancelLoading() {
        if (rl_loading.getVisibility() == VISIBLE) {
            rl_loading.setVisibility(GONE);
        }
    }

    private void showLineChart() {
        if (mLineChart.getVisibility() == GONE) {
            mLineChart.setVisibility(VISIBLE);
        }
    }

    private void hideLineChart() {
        if (mLineChart.getVisibility() == VISIBLE) {
            mLineChart.setVisibility(GONE);
        }
    }

    private void turnToVertical() {
        rg_time_type.setVisibility(VISIBLE);
        rg_time_type_horizontal.setVisibility(GONE);
        rl_right.setVisibility(GONE);
        rg_k_type.setVisibility(VISIBLE);
        rg_bar_type.setVisibility(VISIBLE);
        mLineChart.calculateScale();
    }

    private void performCheckRadioButton() {
        switch (mTimeType) {
            case MIN1:
                ((RadioButton) rg_time_type_horizontal.getChildAt(0)).setChecked(true);
                break;
            case MIN_1:
                ((RadioButton) rg_time_type_horizontal.getChildAt(1)).setChecked(true);
                break;
            case MIN5:
                ((RadioButton) rg_time_type_horizontal.getChildAt(2)).setChecked(true);
                break;
            case MIN15:
                ((RadioButton) rg_time_type_horizontal.getChildAt(3)).setChecked(true);
                break;
            case MIN30:
                ((RadioButton) rg_time_type_horizontal.getChildAt(4)).setChecked(true);
                break;
            case HOUR:
                ((RadioButton) rg_time_type_horizontal.getChildAt(5)).setChecked(true);
                break;
            case HOUR4:
                ((RadioButton) rg_time_type_horizontal.getChildAt(6)).setChecked(true);
                break;
            case DAY:
                ((RadioButton) rg_time_type_horizontal.getChildAt(7)).setChecked(true);
                break;
        }
        switch (mKlineType) {
            case SMA:
                ((RadioButton) rg_k_type_right.getChildAt(0)).setChecked(true);
                break;
            case EMA:
                ((RadioButton) rg_k_type_right.getChildAt(1)).setChecked(true);
                break;
            case BOLL:
                ((RadioButton) rg_k_type_right.getChildAt(2)).setChecked(true);
                break;
            case HJGD:
                ((RadioButton) rg_k_type_right.getChildAt(3)).setChecked(true);
                break;
        }
        switch (mBarLineType) {
            case MACD:
                ((RadioButton) rg_bar_type_right.getChildAt(0)).setChecked(true);
                break;
            case KDJ:
                ((RadioButton) rg_bar_type_right.getChildAt(1)).setChecked(true);
                break;
            case RSI:
                ((RadioButton) rg_bar_type_right.getChildAt(2)).setChecked(true);
                break;
            case MMXH:
                ((RadioButton) rg_bar_type_right.getChildAt(3)).setChecked(true);
                break;
        }
    }

    private void turnToHorizontal() {
        rg_time_type.setVisibility(GONE);
        rg_time_type_horizontal.setVisibility(VISIBLE);
        rl_right.setVisibility(VISIBLE);
        rg_k_type.setVisibility(GONE);
        rg_bar_type.setVisibility(GONE);
        mLineChart.calculateScale();
        if (mKlineMode == KViewMode.MODE_TRAIN) {
            rg_time_type_horizontal.setVisibility(GONE);
        } else {
            rg_time_type_horizontal.setVisibility(VISIBLE);
        }
        performCheckRadioButton();
    }

    public boolean getLinearChartTouch() {
        return mLinearChartToucher;
    }

    public void setMKChartBgColor(List<KTrainRect> mBgList) {
        if (mKlineMode == KViewMode.MODE_TRAIN) {
            mKChart.setBgColor(mBgList);
        }
    }

    public void setUnitWeight(double weight) {
        MyLineChart.mWeight = weight;
    }

    public void setProGroupBean(GoodsAllBean mGroupBean) {
        this.mGroupBean = mGroupBean;
    }
}
